<template>
  <header class="header">
    <div class="header-container">
      <div class="logo">
        <div class="logo-img">
          <el-icon><Monitor /></el-icon>
        </div>
        <h1 class="logo-text">{{ $t('appTitle') }}</h1>
      </div>
      
      <nav class="nav">
        <!-- 首页 -->
        <router-link to="/" class="nav-item" active-class="active">
          <el-icon><House /></el-icon>
          <span>{{ $t('home') }}</span>
        </router-link>
        
        <!-- 关于我们 -->
        <router-link to="/about" class="nav-item" active-class="active">
          <el-icon><InfoFilled /></el-icon>
          <span>{{ $t('about') }}</span>
        </router-link>
        
        <!-- 检测方法 -->
        <router-link to="/detection" class="nav-item" active-class="active">
          <el-icon><Document /></el-icon>
          <span>{{ $t('detection') }}</span>
        </router-link>
        
        <!-- 品质对比 -->
        <router-link to="/quality-comparison" class="nav-item" active-class="active">
          <el-icon><TrendCharts /></el-icon>
          <span>{{ $t('comparison') }}</span>
        </router-link>
        
        <!-- 品质评价 -->
        <router-link to="/quality-evaluation" class="nav-item" active-class="active">
          <el-icon><Star /></el-icon>
          <span>{{ $t('evaluation') }}</span>
        </router-link>
        
        <!-- 数据可视化 -->
        <router-link to="/digital-map" class="nav-item" active-class="active">
          <el-icon><Location /></el-icon>
          <span>{{ $t('map') }}</span>
        </router-link>
        
        <!-- 新闻资讯 -->
        <router-link to="/news" class="nav-item" active-class="active">
          <el-icon><Bell /></el-icon>
          <span>{{ $t('news') }}</span>
        </router-link>
      </nav>
      
      <div class="user-bar">
        <div class="lang-switch" role="group" aria-label="Language switch">
          <button :class="['lang-btn', { active: $lang.language === 'zh' }]" @click="$setLang('zh')">中文</button>
          <button :class="['lang-btn', { active: $lang.language === 'en' }]" @click="$setLang('en')">EN</button>
        </div>
        <template v-if="currentUser">
          <el-dropdown trigger="click" @command="handleUserMenu">
            <button class="user-icon-btn" aria-label="User menu">
              <el-icon><User /></el-icon>
            </button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item disabled>
                  <el-icon style="margin-right:6px"><User /></el-icon>
                  <span class="user-name-dropdown">{{ currentUser.username }}</span>
                </el-dropdown-item>
                <el-dropdown-item v-if="currentUser.type == 0 || currentUser.type == 1" command="admin">
                  <el-icon style="margin-right:6px"><Setting /></el-icon>
                  {{ $t('admin') }}
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon style="margin-right:6px"><SwitchButton /></el-icon>
                  {{ $t('logout') }}
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
        <template v-else>
          <button class="btn-login" @click="$router.push('/login')">
            <el-icon><User /></el-icon>
            {{ $t('login') }}
          </button>
          <button class="btn-register" @click="$router.push('/register')">
            <el-icon><UserFilled /></el-icon>
            {{ $t('register') }}
          </button>
        </template>
      </div>
    </div>
  </header>
</template>

<script>
// 导入所有使用的 Element Plus 图标
import { 
  Monitor, House, InfoFilled, Document, TrendCharts, 
  Star, Location, Bell, User, Setting, SwitchButton, UserFilled
} from '@element-plus/icons-vue'

export default {
  name: 'Header',
  components: {
    // 注册所有导入的图标
    Monitor, House, InfoFilled, Document, TrendCharts,
    Star, Location, Bell, User, Setting, SwitchButton, UserFilled
  },
  data() {
    return {
      currentUser: null
    }
  },
  mounted() {
    this.getCurrentUser()
  },
  methods: {
    getCurrentUser() {
      const user = localStorage.getItem('currentUser')
      this.currentUser = user ? JSON.parse(user) : null
    },
    logout() {
      localStorage.removeItem('currentUser')
      this.currentUser = null
      this.$router.push('/login')
    },
    handleUserMenu(command) {
      if (command === 'admin') {
        this.$router.push('/admin')
      } else if (command === 'logout') {
        this.logout()
      }
    }
  },
  watch: {
    $route() {
      this.getCurrentUser()
    }
  }
}
</script>

<style scoped>
.header {
  background: #fff;
  color: var(--color-primary);
  padding: 1rem 0;
  box-shadow: 0 2px 10px rgba(20,90,50,0.08);
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-container {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2rem;
}
.logo {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}
.logo-img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: var(--color-primary-light);
  padding: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  color: var(--color-primary);
}
.logo-text {
  font-size: 1.5rem;
  font-weight: bold;
  color: var(--color-primary);
  white-space: nowrap;
}
.nav {
  display: flex;
  gap: 0.8rem;
  align-items: center;
  margin-left: 1.5rem;
  flex-wrap: nowrap;
}

.nav-item {
  color: var(--color-primary);
  text-decoration: none;
  padding: 0.5rem 0.8rem;
  border-radius: 8px;
  transition: all 0.3s ease;
  font-weight: 600;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  position: relative;
  border: 1px solid transparent;
  white-space: nowrap;
}

.nav-item:hover,
.nav-item.active {
  background: var(--color-primary-light);
  color: var(--color-primary);
  transform: translateY(-2px);
  border-color: var(--color-primary);
  box-shadow: 0 4px 12px rgba(20, 90, 50, 0.15);
  font-weight: 700;
}

.nav-item:hover .el-icon,
.nav-item.active .el-icon {
  color: var(--color-primary);
  transform: scale(1.1);
}

.nav-item .el-icon {
  font-size: 1rem;
  color: var(--color-primary);
  transition: all 0.3s ease;
}


.user-bar {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  margin-left: 0.8rem;
}

.lang-switch { display: flex; align-items: center; gap: 6px; margin-right: 6px; }
.lang-btn {
  background: #fff;
  color: var(--color-primary);
  border: 1px solid var(--color-primary-light);
  border-radius: 14px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: all .2s ease;
}
.lang-btn:hover { background: var(--color-primary-light); }
.lang-btn.active { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }

.user-info {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.8rem;
  background: var(--color-primary-light);
  border-radius: 18px;
  color: var(--color-primary);
  font-weight: 500;
}

.user-name {
  color: var(--color-primary);
  font-weight: 500;
  font-size: 0.9rem;
  white-space: nowrap;
}
.user-icon-btn {
  background: #fff;
  border: 1px solid var(--color-primary-light);
  color: var(--color-primary);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all .2s ease;
}
.user-icon-btn:hover { background: var(--color-primary-light); }
.user-name-dropdown { color: #666; font-weight: 600; }
.btn-login {
  background: linear-gradient(90deg, #145A32 60%, #218c5c 100%);
  color: #fff;
  border: none;
  border-radius: 20px;
  padding: 0.4rem 1rem;
  font-size: 0.8rem;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(20,90,50,0.08);
  cursor: pointer;
  transition: all 0.2s;
  outline: none;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  white-space: nowrap;
}
.btn-login:hover {
  background: linear-gradient(90deg, #218c5c 60%, #145A32 100%);
  box-shadow: 0 4px 16px rgba(20,90,50,0.13);
  transform: translateY(-1px);
}
.btn-register {
  background: #fff;
  color: var(--color-primary);
  border: 2px solid var(--color-primary-light);
  border-radius: 20px;
  padding: 0.4rem 1rem;
  font-size: 0.8rem;
  font-weight: 500;
  margin-left: 0.6rem;
  box-shadow: 0 2px 8px rgba(20,90,50,0.04);
  cursor: pointer;
  transition: all 0.2s;
  outline: none;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  white-space: nowrap;
}
.btn-register:hover {
  background: var(--color-primary-light);
  color: var(--color-primary);
  border-color: #218c5c;
  transform: translateY(-1px);
}
@media (max-width: 768px) {
  .header-container {
    flex-direction: column;
    gap: 1rem;
  }
  .nav {
    gap: 1rem;
    flex-wrap: wrap;
    justify-content: center;
  }

  .user-bar {
    margin-top: 1rem;
  }
  .user-info {
    padding: 0.4rem 0.8rem;
  }
}
</style> 